package com.tools.editspinner;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListPopupWindow;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import androidx.annotation.ColorInt;
import androidx.core.content.res.ResourcesCompat;


import com.example.login.R;

import java.util.List;

public class EditSpinner extends RelativeLayout implements TextWatcher {
    private EditText editText;
    private ImageView deleteIV;
    private Context mContext;
    private ListPopupWindow popupWindow;
    SimpleAdapter adapter;
    private boolean isPopupWindowShowing;

    public EditSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
        if (popupWindow == null) {
            initPopupWindow();
        }
    }

    public void setDataList(List<String> data) {
        adapter = new SimpleAdapter(mContext,data);
        setAdapter(adapter);
    }

    public void setText(String text) {
        editText.setText(text);
    }

    public void setTextColor(@ColorInt int color) {
        editText.setTextColor(color);
    }

    public String getText() {
        return editText.getText().toString();
    }

    public void setHint(String hint) {
        editText.setHint(hint);
    }

    public void setAdapter(SimpleAdapter adapter) {
        this.adapter = adapter;
        setBaseAdapter(this.adapter);
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.edit_spinner, this);
        editText = (EditText) findViewById(R.id.edit_spinner_edit);
        deleteIV = (ImageView) findViewById(R.id.edit_spinner_delete);
        Drawable search = ResourcesCompat.getDrawable(getResources(), R.drawable.search, null);
        search.setBounds(0, 0, 40, 40);
        editText.setCompoundDrawables(search, null, null, null);
        editText.addTextChangedListener(this);

        deleteIV.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setText("");
                deleteIV.setVisibility(INVISIBLE);
            }
        });
    }

    private final void setBaseAdapter(BaseAdapter adapter) {
        popupWindow.setAdapter(adapter);
    }

    private void initPopupWindow() {
        popupWindow = new ListPopupWindow(mContext) {
            @Override
            public boolean isShowing() {
                return isPopupWindowShowing;
            }

            @Override
            public void show() {
                super.show();
                isPopupWindowShowing = true;
            }
        };
        popupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                editText.setText(((SimpleAdapter) adapterView.getAdapter()).getItemString(i));
                popupWindow.dismiss();
            }
        });
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnchorView(editText);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                isPopupWindowShowing = false;
            }
        });
    }


    @Override
    public final void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public final void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public final void afterTextChanged(Editable s) {
        deleteIV.setVisibility(VISIBLE);
        String key = s.toString();
        editText.setSelection(key.length());
        if (!TextUtils.isEmpty(key)) {
            showFilterData(key);
        } else {
            deleteIV.setVisibility(INVISIBLE);
            popupWindow.dismiss();
        }
    }

    private void showFilterData(String key) {
        if (popupWindow == null || adapter == null || adapter.getEditSpinnerFilter() == null) {
            if (popupWindow != null) {
                popupWindow.dismiss();
            }
            return;
        }
        if (adapter.getEditSpinnerFilter().onFilter(key)) {
            popupWindow.dismiss();
        } else {
            popupWindow.show();
        }
    }
}
